﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ConfigurationManager.View.Dialog;
using System.Diagnostics;

namespace ConfigurationManager.View
{
    public partial class frmInteligenceModuleList : Form
    {
        List<Model.InteligenceModule> _inteligenceModules;
        public frmInteligenceModuleList()
        {
            InitializeComponent();
            this.Load += new EventHandler(frmInteligenceModuleList_Load);
        }

        void frmInteligenceModuleList_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            LoadInteligenceModules();
        }

        private void LoadInteligenceModules()
        {
            _inteligenceModules = Model.InteligenceModule.Retrieve();
            dtgInteligenceModules.DataSource = _inteligenceModules;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmInteligenceModule addIM = new frmInteligenceModule();
            addIM.Owner = this;
            if (addIM.ShowDialog() == DialogResult.OK)
                LoadInteligenceModules();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadInteligenceModules();
        }

        private void dtgInteligenceModules_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;

            if (dtgInteligenceModules.Rows[e.RowIndex].Cells[e.ColumnIndex] == dtgInteligenceModules.Rows[e.RowIndex].Cells["Edit"])
                EditInteligenceModule((Model.InteligenceModule)dtgInteligenceModules.Rows[e.RowIndex].DataBoundItem);
            else if (dtgInteligenceModules.Rows[e.RowIndex].Cells[e.ColumnIndex] == dtgInteligenceModules.Rows[e.RowIndex].Cells["Delete"])
                DeleteInteligenceModule((Model.InteligenceModule)dtgInteligenceModules.Rows[e.RowIndex].DataBoundItem);
        }

        private void dtgInteligenceModules_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            EditInteligenceModule((Model.InteligenceModule)dtgInteligenceModules.Rows[e.RowIndex].DataBoundItem);
        }

        private void DeleteInteligenceModule(ConfigurationManager.Model.InteligenceModule inteligenceModule)
        {
            try
            {
                int id;
                string name;
                id = inteligenceModule.Id;
                name = inteligenceModule.Name;
                if (MessageBox.Show(string.Format("Confirma a exclusão do inteligence module {0} - {1}?", id, name), "Confimar Exclusão", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Model.InteligenceModule.Delete(inteligenceModule.Id);
                    MessageBox.Show("Inteligence Module excluído com sucesso!", "Process Manager Configuration", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    LoadInteligenceModules();
                }
            }
            catch (ValidationException ex)
            {
                MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception ex)
            {
                //EventLog.WriteEntry("InteligenceModule Manager - Configuration Manager", ex.ToString(), EventLogEntryType.Error);
                //MessageBox.Show("Ocorreu um erro interno, verifique o event viewer para maiores detalhes");
                Program.HandleException(ex);
            }
        }

        private void EditInteligenceModule(ConfigurationManager.Model.InteligenceModule inteligenceModule)
        {
            frmInteligenceModule updateIM = new frmInteligenceModule(inteligenceModule);
            updateIM.Owner = this;
            if (updateIM.ShowDialog() == DialogResult.OK)
                LoadInteligenceModules();
        }
    }
}
